package com.gaoyi.crm.fragment.ctm;

import android.os.Bundle;

import androidx.activity.result.ActivityResult;

import com.gaoyi.crm.R;
import com.gaoyi.crm.callback.IView;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.constant.BundleKeyName;
import com.gaoyi.crm.constant.HandleCode;
import com.gaoyi.crm.control.dialog.LoadingDialogUtils;
import com.gaoyi.crm.databinding.FragmentCtmDateBinding;
import com.gaoyi.crm.db.bean.ResultBean;
import com.gaoyi.crm.fragment.base.BaseFragment;
import com.gaoyi.crm.util.DateUtil;
import com.gaoyi.crm.util.SyncDownloadUtil;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.vm.CtmDateViewModel;

import java.util.Date;
import java.util.Map;

public class CtmDateFragment extends BaseFragment<FragmentCtmDateBinding, CtmDateViewModel> implements IView {

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ctm_date;
    }

    @Override
    protected void initView() {
        Date date = new Date();
        mBinding.dateStart.setDate(date, 3);
        mBinding.dateEnd.setDate(date);
        mBinding.btnCancel.setOnClickListener(view -> mContext.onBackPressed());
        mBinding.btnConfirm.setOnClickListener(view -> sync());
    }

    @Override
    protected void initLiveData() {

    }

    private void sync() {
        showLoading();
        SyncDownloadUtil.getInstance().download(mBinding.dateStart.getDate(), mBinding.dateEnd.getDate(), (status, msg) -> {
            hideLoading();
            switch (status) {
                case HandleCode.MSG_DOWNLOAD_TABLE_SUCCESS:
                    Bundle bundle = new Bundle();
                    ResultBean bean = new ResultBean(ApiStatus.SUCCESS, msg, "", CtmDateFragment.class.getSimpleName());
                    bundle.putParcelable(BundleKeyName.RESULT, bean);
                    mController.navigate(R.id.action_ctmDateFragment_to_resultFragment, bundle);
                    break;
                case HandleCode.MSG_DOWNLOAD_TABLE_FAIL:
                    ToastUtil.showToast(msg);
                    break;
            }
        });
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(mContext);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }
}